package com.elite.controller;

import com.alibaba.fastjson.JSON;
import com.elite.entity.*;
import com.elite.service.CommentService;
import com.elite.service.FileService;
import com.elite.service.GoodsorderService;
import com.elite.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/22.
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private GoodsorderService goodsorderService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private FileService fileService;

    private static final Logger LOGGER = Logger.getLogger(CommentController.class);

    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    @ResponseBody
    public void addComment(Integer goodsorderId,String commentComments){

        Goodsorder goodsorder = goodsorderService.selectByPrimaryKey(goodsorderId);
        Comment comment = new Comment();
        comment.setGoodsId(goodsorder.getGoodsId());
        comment.setSellerId(goodsorder.getSellerId());
        comment.setCommentComments(commentComments + "," + goodsorder.getUserId());
        commentService.addComment(comment);

    }

    @RequestMapping(value = "/showComment",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String showComment(Integer goodsId){

        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String,Object> map = new HashMap<String, Object>();
        List<Comment> comments = null;
        try{
            comments = commentService.selectComment(goodsId);
        }catch (NullPointerException e){
            e.printStackTrace();
        }

        if (comments != null && comments.size() != 0){
            for (Comment comment : comments){
                String com = comment.getCommentComments();
                String[] arr = com.split(",");
                Integer userId = Integer.valueOf(arr[arr.length - 1]);
       LOGGER.info("----------------->>>>>>>>>>>,,,userid"+userId);
                String Comments = com.substring(0,com.lastIndexOf(",")-1);
                User user = userService.selectById(userId);

                FileGoods fileGoods = null;
                try {
                    fileGoods = fileService.selectByUserId(userId);
                }catch (NullPointerException e){
                    e.printStackTrace();
                }

                File file = null;
                if (fileGoods != null){
                    file = fileService.selectByPrimaryKey(fileGoods.getFileId());
                }
                if (file != null){
LOGGER.info("----------------->>>>>>>>>>>,,,"+file.getFileUrl());
                    map.put("fileUrl",file.getFileUrl());
                    map.put("userNikename",user.getUserNikename());
                    map.put("Comments",Comments);
                    mapList.add(map);

                }else {

                    map.put("fileUrl","img\\user.png");
                    map.put("userNikename",user.getUserNikename());
                    map.put("Comments",Comments);
                    mapList.add(map);

                }


            }
        }
        return JSON.toJSON(mapList).toString();
    }
}
